# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=openssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_ver=1.0.2o
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=1
arch=('any')
pkgdesc="The Open Source toolkit for Secure Sockets Layer and Transport Layer Security (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates" "${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' '!buildflags' 'staticlibs')
license=('BSD')
url="https://www.openssl.org"
noextract=(${_realname}-${_ver}.tar.gz)
source=(https://www.openssl.org/source/${_realname}-${_ver}.tar.gz{,.asc}
        'openssl-1.0.0a-ldflags.patch'
        'openssl-1.0.2a-parallel-build.patch'
        'openssl-1.0.1-x32.patch'
        'openssl-0.9.6-x509.patch'
        'openssl-1.0.1i-relocation.patch')
sha256sums=('ec3f5c9714ba0fd45cb4e087301eb1336c317e0d20b575a125050470e8089e4d'
            'SKIP'
            '164aa4928b022cc716fac545b4fd69899cb274682aa487100e595abb652adbae'
            '609d7ca040f7ab26f5e9844e486b3bcc04f3da656ce2db7733fbd65c6d10457a'
            'f913185a36538315de3ff122ef96fc9a49e068078e65895cfc950bdb4ac4e4f0'
            'e8ed15b50f749064e89f26d49b7fd85b3f95e5dde22e430c0f6181e5b07146c7'
            'f8e6233fa4c289fb125586bf3f30cd617923f6be0c6fe8a3289aa3fac7d7e814')

validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491')

prepare() {
  # Clean up old sources so re-patching doesn't fail.
  [[ -d ${srcdir}/${_realname}-${_ver} ]] && rm -rf ${srcdir}/${_realname}-${_ver}
  tar -xzvf ${_realname}-${_ver}.tar.gz -C ${srcdir} || true
  cd ${srcdir}/${_realname}-${_ver}

  patch -p1 -i ${srcdir}/openssl-1.0.0a-ldflags.patch
  patch -p1 -i ${srcdir}/openssl-1.0.2a-parallel-build.patch
  patch -p1 -i ${srcdir}/openssl-1.0.1-x32.patch
  patch -p1 -i ${srcdir}/openssl-0.9.6-x509.patch
  patch -p1 -i ${srcdir}/openssl-1.0.1i-relocation.patch

}

build() {
  # No support for out-of-source builds
  mkdir -p ${srcdir}/build-${CARCH}
  cp -a ${srcdir}/${_realname}-${_ver}/* ${srcdir}/build-${CARCH}

  # Use mingw cflags instead of hardcoded ones
  sed -i -e '/^"mingw"/ s/-fomit-frame-pointer -O3 -march=i486 -Wall/-O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4/' ${srcdir}/build-${CARCH}/Configure

  _mingw=mingw
  if $(echo ${CARCH} | grep -q x86_64); then
    _mingw=mingw64
  fi
  cd "${srcdir}/build-${CARCH}"
  export MSYS2_ARG_CONV_EXCL="--prefix="
  ./Configure \
    --prefix=${MINGW_PREFIX} \
    ${_mingw} \
    shared \
    zlib-dynamic \
    enable-camellia \
    enable-idea \
    enable-mdc2 \
    enable-tlsext \
    enable-rfc3779

  make -j1 ZLIB_INCLUDE=-I"${MINGW_PREFIX}"/include depend all
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make -j1 INSTALL_PREFIX="${pkgdir}" MANDIR=${MINGW_PREFIX}/share/man MANSUFFIX=ssl install

  install -D -m644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  chmod -R 755 "${pkgdir}${MINGW_PREFIX}"/bin
  chmod -R 644 "${pkgdir}${MINGW_PREFIX}"/lib
  chmod -R 755 "${pkgdir}${MINGW_PREFIX}"/lib/engines

  install -m644 ms/applink.c "${pkgdir}${MINGW_PREFIX}"/include/openssl/
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/c_rehash
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{ssl/misc,ssl/man}
}
